package com.sakura.mall.product.category.controller;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.common.response.R;
import com.sakura.mall.product.category.domain.request.AddCategoryRequest;
import com.sakura.mall.product.category.domain.request.UpdateCategoryRequest;
import com.sakura.mall.product.category.domain.request.CategoryPageRequest;
import com.sakura.mall.product.category.service.CategoryService;
import com.sakura.mall.product.category.domain.vo.CategoryVo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 商品三级分类
 *
 * @author sakura
 */
@RestController
@RequestMapping("/category")
@RequiredArgsConstructor
public class CategoryController {

    private final CategoryService categoryService;


    /**
     * 新建
     *
     * @return R {@link Void}
     */
    @PostMapping
    public R<Void> create(@RequestBody @Validated AddCategoryRequest request) {
        categoryService.add(request);
        return R.ok();
    }


    /**
     * 分页获取
     *
     * @return R {@link Page<CategoryVo>}
     */
    @GetMapping("list")
    public R<Page<CategoryVo>> page(CategoryPageRequest request) {
        Page<CategoryVo> ret = categoryService.queryPage(request);
        return R.ok(ret);
    }


    /**
     * 根据id删除
     *
     * @return R {@link Void}
     */
    @DeleteMapping("{id}")
    public R<Void> remove(@PathVariable Long id) {
        categoryService.removeById(id);
        return R.ok();
    }


    /**
     * 根据id更新
     *
     * @return R {@link Void}
     */
    @PostMapping("{id}")
    public R<Void> update(@PathVariable Long id, @RequestBody UpdateCategoryRequest request) {
        categoryService.updateById(id, request);
        return R.ok();
    }

}
